package it.codelair.database;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public class Connection {
	private String url;
	private List<NameValuePair> parametri;
	
	public Connection(){
		this.url = "";
		this.parametri = new ArrayList<NameValuePair>();
	}
	
	public Connection(String url){
		this.setUrl(url);
		this.parametri = new ArrayList<NameValuePair>();
	}
	
	public String connect() throws ClientProtocolException, IOException{
		HttpClient client = new DefaultHttpClient();
    	HttpPost httpPost = new HttpPost(this.url);
    	HttpResponse responseGet;
        httpPost.setEntity(new UrlEncodedFormEntity(this.parametri));
		responseGet = client.execute(httpPost);
		HttpEntity responseEntity = responseGet.getEntity();
		String response_str =new String(EntityUtils.toString(responseEntity, HTTP.UTF_8));
		return response_str;
//		if(response_str!=null) {
//			Integer result = new Integer(Integer.parseInt(response_str));
//			return result;
//		}
//		else {
//			return null;
//		}
			
	}
	
	public void addParameter(String name,String value){
		this.parametri.add(new BasicNameValuePair(name, value));
	}

	public List<NameValuePair> getParametri() {
		return parametri;
	}

	public void setParametri(List<NameValuePair> parametri) {
		this.parametri = parametri;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
}
